﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace Labb2
{
    class SmokeParticle
    {
        int m_seed;
        Texture2D smokeTexture;
        Random rand;
        Camera _camera;
        Vector2 _startPosition;
        Vector2 _smokePosition;
        Vector2 _originalVelocity;
        Vector2 _velocity;
        Vector2 _acceleration;
        float _diameter;
        float _LifeCycle;
        float _decaySpeed;
        float _maxLifeTime = 1f;
        float _delay;

        public SmokeParticle(Vector2 startingPosition, int seed, Texture2D texture, Camera camera) 
        {
            m_seed = seed;
            _camera = camera;
            rand = new Random(seed);

            _diameter = 0.3f;
            _LifeCycle = 0f;
            _decaySpeed = (float)rand.NextDouble();
            
            _delay = ((float)rand.NextDouble() * 10);

            smokeTexture = texture;

            _startPosition = startingPosition;
            _smokePosition = new Vector2(_startPosition.X, _startPosition.Y);
            //_velocity = new Vector2((float)(rand.NextDouble() * 2.0 - 1.0), (float)(rand.NextDouble() * 2.0 - 1.0));
            _velocity = new Vector2((float)(rand.NextDouble() * 2.0 - 1.0), -1f);
            _velocity.Normalize();
            _originalVelocity = new Vector2(_velocity.X, _velocity.Y);

            _acceleration = new Vector2(0.0f, -5.1f);

            //float min = 1.001f;
            //float max = 2.01f;
            float min = 0.5f;
            float max = 1f;
            float particleSpeed = min + ((float)(rand.NextDouble()) * (max - min));

            _acceleration *= particleSpeed;
        }

        internal void Update(GameTime gameTime) 
        {
            //Vector2 randomDirection = new Vector2((float)rand.NextDouble() - 1.5f, (float)rand.NextDouble() - 1.5f);
            //randomDirection.Normalize();
            if(_delay < 10f)
            {
                _delay += 0.5f * _decaySpeed;
            }
            else 
            {
                _smokePosition += (_velocity) * gameTime.ElapsedGameTime.Milliseconds / 7000;
                _velocity += _acceleration * gameTime.ElapsedGameTime.Milliseconds / 5000;

                if (_LifeCycle >= _maxLifeTime)
                {
                    Rebirth();
                }
            
                _LifeCycle += 0.007f * _decaySpeed;
            }
            //Vector2 newVelocity = new Vector2();
            //System.Diagnostics.Debug.WriteLine("doge Number: " + m_seed + "Position: " + m_dogePosition);
            //newVelocity.X = gameTime.ElapsedGameTime.Seconds * 

        }

        internal void Draw(SpriteBatch spritebatch) 
        {
            if(_delay >= 10f)
            {
                //int diameter = m_camera.GetVisualCoords(m_diameter);
                int xCoord = (int)_camera.GetVisualCoords(_smokePosition).X;
                int yCoord = (int)_camera.GetVisualCoords(_smokePosition).Y;

                //Vector2 size = m_camera.GetVisualCoords(new Vector2(m_diameter, m_diameter));
                //int width = (int)size.X;
                //int height = (int)size.Y;

                int width = (int)((_camera.GetVisualCoords(new Vector2(_diameter, _diameter)).X) * _LifeCycle);
                int height = (int)((_camera.GetVisualCoords(new Vector2(_diameter, _diameter)).Y) * _LifeCycle);
                Rectangle smokeRectangle = new Rectangle(xCoord, yCoord, width, height);
                //spritebatch.Draw(smokeTexture, smokeRectangle, Color.White);

                float fade = (1f - _LifeCycle);
                Color color = new Color(fade, fade, fade, fade);
                float scale = 3f + _LifeCycle * (_decaySpeed * 10);
                float rotation = _LifeCycle * 10;

                //spritebatch.Draw(smokeTexture, new Vector2(xCoord, yCoord), null, color, rotation, new Vector2(6f, 6f), scale, SpriteEffects.None, 0);
                spritebatch.Draw(smokeTexture, smokeRectangle, null, color, rotation, new Vector2(5, 5), SpriteEffects.None, 0f);
                //System.Diagnostics.Debug.WriteLine("Bredd: " + width + " Höjd: " + height);
                //System.Diagnostics.Debug.WriteLine(_camera.GetVisualCoords(new Vector2((int)_diameter, (int)_diameter)).X);
            }
        }

        internal void Rebirth() 
        {
            _smokePosition = new Vector2(_startPosition.X, _startPosition.Y);
            _LifeCycle = 0f;
            _velocity = new Vector2(_originalVelocity.X, _originalVelocity.Y);
        }
    }
}
